package com.ywu.standard.gateway.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.ywu.common.utils.AesUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName JwtUtils
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/10 17:28
 * @Version 1.0
 **/
@Slf4j
public class JwtUtils {

    /**
    * @Author GroundDemo
    * @Description 创建Token
    * @Date 17:29 2024/3/10
    * @Param [userAccount, authentications, secret, expire]
    * @return java.lang.String
    **/
    public static String createToken(String userAccount, String tenantId, String secret, long expire){
        return JWT.create().withAudience(userAccount)
                .withIssuedAt(new Date())
                .withExpiresAt(new Date(System.currentTimeMillis()+ expire))
                .withClaim("authentications", userAccount)
                .withClaim("tenantId", tenantId)
                .sign(Algorithm.HMAC256(secret));
    }

    /**
     * @Author GroundDemo
     * @Description 创建Token
     * @Date 17:29 2024/3/10
     * @Param [userAccount, authentications, secret, expire]
     * @return java.lang.String
     **/
    public static String createAppInfoToken(String userAccount, String appInfo, String secret, long expire){
        return JWT.create().withAudience(userAccount)
                .withIssuedAt(new Date())
                .withExpiresAt(new Date(System.currentTimeMillis()+ expire))
                .withClaim("userAppInfo", appInfo)
                .sign(Algorithm.HMAC256(secret));
    }

    /**
    * @Author GroundDemo
    * @Description openService服务调用创建Token
    * @Date 11:44 2024/9/28
    * @Param [appId, appToken, source, secret, expire]
    * @return java.lang.String
    **/
    public static String createOpenServiceToken(String appId, String appToken, String source, String secret, long expire){
        return JWT.create().withAudience(appId)
                .withIssuedAt(new Date())
                .withExpiresAt(new Date(System.currentTimeMillis()+ expire))
                .withClaim("appId", appId)
                .withClaim("appToken", appToken)
                .withClaim("source", source)
                .sign(Algorithm.HMAC256(secret));
    }

    /**
    * @Author GroundDemo
    * @Description 解析Token
    * @Date 18:04 2024/3/10
    * @Param [token, secret]
    * @return java.lang.String
    **/
    public static Map<String, String> decodeToken (String token, String secret) {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
        DecodedJWT jwt = verifier.verify(token);
        Map<String, Claim> claims = jwt.getClaims();
        Map<String, String> res = new HashMap<>();
        for (String key : claims.keySet()) {
            Claim claim = claims.get(key);
            String value = claim.toString();
            if (value.startsWith("\"") && value.endsWith("\"")) {
                value = value.substring(1, value.length() - 1);
            }
            res.put(key, value);
        }
        return res;
    }

//    public static void main(String[] args) throws Exception {
//        String secretKey = "qoMcUkEm4QWN+FQ+dRLTuQ==";
//        String tokenKey = "NBrHeXxOOMk8GVzvPNEsJGVIWtsRkrVyh2qmesMbJLM=";
//        String decrypt = AesUtils.decrypt(tokenKey, secretKey);
//        System.out.println(decrypt);
//        String token = createToken("ywu", decrypt, 36);
////        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJ5d3UiLCJleHAiOjE3MTA1OTg2MTAsImlhdCI6MTcxMDU5ODYxMCwiYXV0aGVudGljYXRpb25zIjoieXd1In0.p_pbqkwW1_AWmOhLTDD7DJIP8YPIgc8eBaly21m_VLk";
//        System.out.println(token);
//        Map<String, String> ywu = decodeToken(token, decrypt);
//        System.out.println(ywu);
//    }
}
